// pages/shopOrders/orderList/orderList.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
        orderType: 0,
        orderList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            orderList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getOrderList()
    },

    getUserInfo: function() {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
                that.getOrderList()
            }
        }).catch(function(e) {})
    },

    getOrderList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let data
        let orderList = this.data.orderList
        if (this.data.userInfo.headquarters == 1) {
            if (this.data.orderType == 0) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.orderType == 1) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 0,
                    shipping_status: 0,
                    pay_status: 0,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.orderType == 2) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 0,
                    shipping_status: 0,
                    pay_status: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.orderType == 3) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    virtual_goods: 0,
                    paramter: 1,
                    goods_client: 2,
                    order_status: 0,
                    shipping_status: 1,
                    pay_status: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.orderType == 4) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 1,
                    shipping_status: 2,
                    pay_status: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.orderType == 5) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            }
        } else {
            if (this.data.orderType == 0) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.orderType == 1) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 0,
                    shipping_status: 0,
                    pay_status: 0,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.orderType == 2) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 0,
                    shipping_status: 0,
                    pay_status: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.orderType == 3) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 0,
                    shipping_status: 1,
                    pay_status: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.orderType == 4) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 1,
                    shipping_status: 2,
                    pay_status: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.orderType == 5) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            }
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Orderlist/user_order', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    orderList: orderList,
                    isAll: true
                })
            } else {
                orderList = orderList.concat(res)
                that.setData({
                    orderList: orderList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    switchOrderType: function(e) {
        if (this.data.orderType == e.currentTarget.dataset.type) {
            return
        }
        this.setData({
            orderType: e.currentTarget.dataset.type,
            nowPage: 0,
            isAll: false,
            isLoading: false,
            orderList: []
        })
        this.getOrderList()
    },

    goOrderDetail: function(e) {
        wx.navigateTo({
            url: '/pages/shopOrders/orderDetail/orderDetail?orderId=' + e.currentTarget.dataset.id
        })
    },

    goLogistics: function(e) {
        wx.navigateTo({
            url: '/pages/order/orderLogistics/orderLogistics?expressId=' + e.currentTarget.dataset.id
        })
    },

    goGoodsDetail: function(e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
        })
    },

    updateData: function() {
        const that = this
        let data
        if (this.data.userInfo.headquarters == 1) {
            if (this.data.orderType == 0) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            } else if (this.data.orderType == 1) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 0,
                    shipping_status: 0,
                    pay_status: 0,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            } else if (this.data.orderType == 2) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 0,
                    shipping_status: 0,
                    pay_status: 2,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            } else if (this.data.orderType == 3) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    virtual_goods: 0,
                    paramter: 1,
                    goods_client: 2,
                    order_status: 0,
                    shipping_status: 1,
                    pay_status: 2,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            } else if (this.data.orderType == 4) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 1,
                    shipping_status: 2,
                    pay_status: 2,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            } else if (this.data.orderType == 5) {
                data = {
                    get_user: 1,
                    goods_store_id: 2,
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 2,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            }
        } else {
            if (this.data.orderType == 0) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            } else if (this.data.orderType == 1) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 0,
                    shipping_status: 0,
                    pay_status: 0,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            } else if (this.data.orderType == 2) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 0,
                    shipping_status: 0,
                    pay_status: 2,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            } else if (this.data.orderType == 3) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 0,
                    shipping_status: 1,
                    pay_status: 2,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            } else if (this.data.orderType == 4) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 1,
                    shipping_status: 2,
                    pay_status: 2,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            } else if (this.data.orderType == 5) {
                data = {
                    get_user: 1,
                    store_id: wx.getStorageSync('storeId'),
                    paramter: 1,
                    goods_client: 2,
                    virtual_goods: 0,
                    order_status: 2,
                    limit: 10 * this.data.nowPage,
                    page: 0
                }
            }
        }
        wx.showLoading({
            title: '加载中',
        })
        common.post('Orderlist/user_order', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    orderList: []
                })
            } else {
                that.setData({
                    orderList: res
                })
            }
        }).catch(function(e) {})
    }
})